home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktimeintro / desktop sprites / completed lab / createsprites.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-06  |  3.1 KB  |  117 lines

  1.  
  2.  
  3. #ifndef _MAININCLUDES_
  4. #include "main.h"
  5. #endif
  6.  
  7. void CreateSpriteStuff (Rect *windowBounds, CGrafPtr windowPtr)
  8. {
  9.     OSErr err;
  10.     Rect bounds;
  11.  
  12.     // calculate the size of the destination
  13.     bounds = *windowBounds;
  14.     OffsetRect (&bounds, -bounds.left, -bounds.top);
  15.     gBounceBox = bounds;
  16.     InsetRect (&gBounceBox, 16, 16);
  17.  
  18.     // create a sprite layer graphics world with a bit depth of 32
  19.     NewGWorld (&gSpritePlane, 32, &bounds, nil, nil, useTempMem);
  20.     if (gSpritePlane == nil)
  21.     {
  22.         NewGWorld (&gSpritePlane, 32, &bounds, nil, nil, 0);
  23.     }
  24.     
  25.     if (gSpritePlane)
  26.     {
  27.         LockPixels (GetPortPixMap(gSpritePlane));
  28.         
  29.         gBackgroundColor.red = 0x1234;
  30.         gBackgroundColor.green = 0x0001;
  31.         gBackgroundColor.blue = 0x0044;
  32.         
  33.  
  34.         // create a sprite world
  35.         err = NewSpriteWorld (&gSpriteWorld,        /* on return, a new sprite world */
  36.                                 windowPtr,            /* destination */
  37.                                 gSpritePlane,        /* sprite layer graphics world */
  38.                                 &gBackgroundColor,    /* background color */
  39.                                 nil);                /* graphics world to be used as the background. */
  40.         
  41.         // create sprites
  42.         CreateSprites();
  43.     }
  44. }
  45.  
  46. void CreateSprites (void)
  47. {
  48.     long                lIndex;
  49.     Handle                hCompressedData = NULL;
  50.     PicHandle            hpicImage;
  51.     OSErr                nErr;
  52.     RGBColor            rgbcKeyColor;
  53.     
  54.     SetRect(&gDestRects[0], 132, 132, 132 + kSpaceShipWidth, 
  55.         132 + kSpaceShipHeight);
  56.     SetRect(&gDestRects[1], 50, 50, 50 + kSpaceShipWidth, 
  57.         50 + kSpaceShipHeight);
  58.     SetRect(&gDestRects[2], 100, 100, 100 + kSpaceShipWidth, 
  59.         100 + kSpaceShipHeight);
  60.     SetRect(&gDestRects[3], 130, 130, 130 + kSpaceShipWidth, 
  61.         130 + kSpaceShipHeight);
  62.  
  63.     gDeltas[0].h = -3;
  64.     gDeltas[0].v = 0;
  65.     gDeltas[1].h = -5;
  66.     gDeltas[1].v = 3;
  67.     gDeltas[2].h = 4;
  68.     gDeltas[2].v = -6;
  69.     gDeltas[3].h = 6;
  70.     gDeltas[3].v = 4;
  71.     
  72.     gCurrentImages[0] = 0;
  73.     gCurrentImages[1] = kNumSpaceShipImages / 4;
  74.     gCurrentImages[2] = kNumSpaceShipImages / 2;
  75.     gCurrentImages[3] = kNumSpaceShipImages * 4 / 3;
  76.     
  77.     rgbcKeyColor.red = 0;
  78.     rgbcKeyColor.green = 0;
  79.     rgbcKeyColor.blue = 0;
  80.     
  81.     // recompress PICT images to make them transparent
  82.     for (lIndex = 0; lIndex < kNumSpaceShipImages; lIndex++) 
  83.     {
  84.         ImageDescriptionHandle idh=nil;
  85.         Handle  imageData=nil;
  86.  
  87.         hpicImage = (PicHandle)GetPicture(lIndex +
  88.                                             kFirstSpaceShipPictID);
  89.         DetachResource((Handle)hpicImage);
  90.  
  91.         nErr = RecompressPictureWithTransparency(hpicImage,
  92.                                                 &rgbcKeyColor, 
  93.                                                 nil,
  94.                                                 &gImageDescriptions[lIndex],
  95.                                                 &gCompressedPictures[lIndex]);
  96.         KillPicture(hpicImage);
  97.     }
  98.  
  99.     // create the sprites for the sprite world
  100.     for (lIndex = 0; lIndex < kNumSprites; lIndex++) {
  101.         MatrixRecord    matrix;
  102.  
  103.         SetIdentityMatrix(&matrix);
  104.         
  105.         matrix.matrix[2][0] = ((long)gDestRects[lIndex].left << 16);
  106.         matrix.matrix[2][1] = ((long)gDestRects[lIndex].top << 16);
  107.  
  108.         nErr = NewSprite(&(gSprites[lIndex]),            /* on return, the ID of the new sprite */
  109.                         gSpriteWorld,                    /* the sprite world for this sprite */
  110.                         gImageDescriptions[lIndex],        /* image description of the sprite’s image. */
  111.                         *gCompressedPictures[lIndex],     /* sprite image data */
  112.                         &matrix,                        /* sprite matrix */
  113.                         true,                            /* is sprite visible? */
  114.                         lIndex);                         /* sprite layer */
  115.  
  116.     }
  117. }